const express = require('express')
const path = require('path')
const userRoutes = require('./routes/user')
const messageRoutes = require('./routes/msg')
const indexRoutes = require('./routes/index')
const bodyParser = require('body-parser')
const session = require('express-session')
const FileStore = require('session-file-store')(session)
const sessionConfig = require('./config/session')
const cookieParser = require('cookie-parser')

const app = express()

app.use(function (req, res, next) {
  // console.log('Start Time:', Date.now() / 1000)
  next()
})

// 设置主目录
app.use(express.static(path.join(__dirname, 'dist')))
// 解析post信息
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
// session 相关
app.use(cookieParser())
app.use(session({
  name: sessionConfig.name,
  secret: sessionConfig.secret,
  store: new FileStore(), // 本地存储session（文本文件，也可以选择其他store，比如redis的）
  saveUninitialized: sessionConfig.saveUninitialized,
  resave: sessionConfig.resave,
  cookie: sessionConfig.cookie
}))

app.use('/', indexRoutes)
app.use('/user', userRoutes)
app.use('/msg', messageRoutes)

let server = app.listen(80, function () {
  let host = server.address().address
  let port = server.address().port
  console.log('应用实例，访问地址为 http://%s:%s', host, port)
})
